var chargeType = function() {
    var self = this;
    self.thirds = [];
    self.banks = [];
    self.alipay = [];
    self.wechat = [];
    self.cft = [];
};
chargeType.prototype = {};

chargeType.prototype.init = function() {
    var self = this;
    Classify.query("settings", "charge_enable", false, "1", function(v) {
        if (v == '0') {
            $(".charge-c .disable").show();
            return;
        }

        ME.query(function() {
            self.grade = context.user.user.layer || '0';
            self.grade = self.grade + "";
            self.queryList();
        });
    });

    // 按钮点击的处理
    $(".bank-charge").click(function() {
        Util.jumpUrl({
            cmd: "charge_bank",
        })
    });
    $(".alipay-charge").click(function() {
        Util.jumpUrl({
            cmd: "charge_alipay",
        })
    });
    $(".alipay-online-charge").click(function() {
        Util.jumpUrl({
            cmd: "charge_alipay_online",
        })
    });
    $(".wechat-charge").click(function() {
        Util.jumpUrl({
            cmd: "charge_wechat",
        })
    });
    $(".wechat-online-charge").click(function() {
        Util.jumpUrl({
            cmd: "charge_wechat_online",
        })
    });
    $(".qq-charge").click(function() {
        Util.jumpUrl({
            cmd: "charge_qq",
        })
    });
    $(".qq-online-charge").click(function() {
        Util.jumpUrl({
            cmd: "charge_qq_online",
        })
    });
    $(".bank-online-charge").click(function() {
        Util.jumpUrl({
            cmd: "charge_bank_online",
        })
    });
}

chargeType.prototype.queryList = function() {
    var self = this;
    var grade = self.grade;

    // 第三方
    CMD.pay_uquery(function(r) {
        for (var i in r.list) {
            var data = r.list[i];
            if (data.enable != 1) continue;
            data.layer = data.layer.split(",");
            if (data.layer.indexOf(grade) == -1) continue;
            self.thirds.push(data);
        }
        self.update();
    });

    // 银行转账
    Classify.queryExt("money", "banks", true, [], function(r) {
        for (var i in r) {
            if (r[i].enable != 1) continue;
            if (r[i].layer.indexOf(grade) == -1) continue;
            self.banks.push(r[i]);
        }
        self.update();
    });

    // 支付宝转账
    Classify.queryExt("money", "alipay", true, [], function(r) {
        for (var i in r) {
            if (r[i].enable != 1) continue;
            if (r[i].layer.indexOf(grade) == -1) continue;
            self.alipay.push(r[i]);
        }
        self.update();
    });

    // 微信转账
    Classify.queryExt("money", "wechat", true, [], function(r) {
        for (var i in r) {
            if (r[i].enable != 1) continue;
            if (r[i].layer.indexOf(grade) == -1) continue;
            self.wechat.push(r[i]);
        }
        self.update();
    });

    // 财付通
    Classify.queryExt("money", "cft", true, [], function(r) {
        for (var i in r) {
            if (r[i].enable != 1) continue;
            if (r[i].layer.indexOf(grade) == -1) continue;
            self.cft.push(r[i]);
        }
        self.update();
    });
}

chargeType.prototype.update = function() {
    var self = this;

    // 在线网银
    var flag = false;
    for (var i in self.thirds) {
        var d = self.thirds[i];
        if (d.payType == 'bank') {
            flag = true; break;
        }
    }
    if (flag) $(".bank-online-charge").show();
    else $(".bank-online-charge").hide();

    // 银行转账
    var flag = self.banks.length > 0;
    if (flag) $(".bank-charge").show();
    else $(".bank-charge").hide();

    // 支付宝
    var flag = self.alipay.length > 0;
    if (flag) $(".alipay-charge").show();
    else $(".alipay-charge").hide();

    var flag = false;
    for (var i in self.thirds) {
        var d = self.thirds[i];
        if (d.payType == 'alipay') {
            flag = true; break;
        }
    }
    if (flag) $(".alipay-online-charge").show();
    else $(".alipay-online-charge").hide();

    // 微信
    var flag = self.wechat.length > 0;
    if (flag) $(".wechat-charge").show();
    else $(".wechat-charge").hide();

    var flag = false;
    for (var i in self.thirds) {
        var d = self.thirds[i];
        if (d.payType == 'wechat') {
            flag = true; break;
        }
    }
    if (flag) $(".wechat-online-charge").show();
    else $(".wechat-online-charge").hide();
    

    // QQ
    var flag = self.cft.length > 0;
    if (flag) $(".qq-charge").show();
    else $(".qq-charge").hide();
    
    var flag = false;

    for (var i in self.thirds) {
        var d = self.thirds[i];
        if (d.payType == 'qq') {
            flag = true; break;
        }
    }
    if (flag) $(".qq-online-charge").show();
    else $(".qq-online-charge").hide();

    if (window._mainScroll) _mainScroll.refresh();
}

window.ChargeType = new chargeType();
